<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@include file="included/commons.jsp"%>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
}
-->
</style>
<title>
部门选择器
</title>
</head>
<body onload="sendRequest('department');">
<table width="200" height="119" border="0" cellpadding="0"
	cellspacing="1" bgcolor="#000000">
<tr>
<th width="100%" height="25" bgcolor="#999999" scope="col">
<p>
请选择部门
</th>
</tr>
<tr>
<td height="18" align="center" bgcolor="#FFFFFF" scope="col">
<select name="typeId" size="10" id="xdept"
	onChange="showSelectedValue(this.name,'msg')">
</select>
</td>
</tr>
<tr>
<td height="19" align="center" bgcolor="#FFFFFF" scope="col">
<input type="text" id="msg" name="msg" />
</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF" scope="col">
<input type="button" name="buttonsa" value="提交"
	onClick="submitForm('${param.id}')" />
<label></label>
</td>
</tr>
</table>
</form>
<script language="javascript">
    var xmlhttp=null;
    var msg = document.getElementById("msg");
    var dept = document.getElementById("xdept");  
    
    function showSelectedValue(objName,msg){
        var msgx = document.getElementById(msg);
        var selObj = document.getElementById(objName);
        msgx.value = selObj.options[selObj.selectedIndex].text;
    }
    
    function clearOptions(objName){
        var selObj = document.getElementById(objName);
        while(selObj.options.length > 0)
           selObj.remove(0);
    
    }   
    function sendRequest(userType){
        clearOptions("xdept");
        try{        
            xmlhttp = new XMLHttpRequest();
        }catch(e){
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url = "${base}selectuser.action?userType="+userType+"&typeName=input";
        //alert(url);
        xmlhttp.onreadystatechange = callback;
        xmlhttp.open("Post",url,true);
        //xmlhttp.send();
        //xmlhttp.send()方法发送的数据为空时，在server端Servlet会以doGet()方法来处理
        xmlhttp.send();
        //xmlhttp.send(data)方法发送的data数据非空时，在server端Servlet会以doPost()方法来处理
        
    }//end of  xmlHttpRequest
function callback(){
        //alert(xmlhttp.readyState);
        if(xmlhttp.readyState == 4)  {
        //返回XMLHttp请求的当前状态:4 (完成) ;
		//数据接收完毕,此时可以通过通过responseBody和responseText获取完整的回应数据
       
            var returnContent = xmlhttp.responseText;
            //alert(returnContent);
            if(xmlhttp.status == 200) {
               //返回当前请求的http状态码:200 OK
               // document.write(xmlhttp.responseText);
                 processXML(returnContent);
              
            }else{
                 alert("returnContent="+returnContent);
            }
        
        }
        
    }//end of callback
    function processXML(xml){
        //var xml="<?xml version='1.0' encoding='utf-8'?><p><id>hello</id><name first='yes'>linda</name></p>";
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        
        //alert(xml);
        xmlDoc.loadXML(xml);        
        
        //dept.remove(dept.options[j]);
        var node = xmlDoc.documentElement;          
          for(var i=0;i<node.childNodes.length;i++){
              if(node.childNodes[i].nodeName=="userext"){
                  var insuredList = node.childNodes[i];
                  var insured = insuredList.childNodes;
                  var opt = new Option();
                  for(var index=0;index<insured.length;index++){
				       if(insured[index].nodeName == "id"){
				           opt.value = insured[index].getAttribute("value");
				       }
				       if(insured[index].nodeName == "name"){
				           opt.text = insured[index].getAttribute("value");
				       }
                       
                  }// end of inner for loop  
                  
               }  
               dept.add(opt);           
           }//end of outter for loop
	  } //end of function parseXML

	  function setFunction(){
	     return true;
	  }
	  
	  function submitForm(id){
	     opener.document.getElementById(id).value = msg.value;
	     window.close();
	  }
</script>

</body>
</html>
